package com.dreamcode.caloriestracker.activities;

import com.dreamcode.caloriestracker.collectors.ProfileDAO;
import com.dreamcode.caloriestracker.collectors.ProfileDAOimpl;
import com.dreamcode.caloriestracker.modelclass.ProfileDetails;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ProfileCreationActivity extends Activity{
	
	private EditText editLabel = null;
	private Spinner spinner = null;
	private Spinner spinnerWe = null;
	private Spinner spinnerHe = null;
	private Spinner spinnerWa = null;
	private EditText editAge = null;
	private EditText editWeight = null; 
	private EditText editHeight = null; 
	private EditText editWaist = null; 
	
	private Button btnOk = null;
	private Button btnCancel = null;
	private ProfileDAO profileDAO = null;
	private ProfileDetails profileDetails = null;
	private boolean editMode = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.profileinfocreation);
		initView();
	}
	public void initView(){
		profileDAO = ProfileDAOimpl.getInstance();
		String [] spinnerValues = {"Male","Female"};
		String [] spinnerWeValues = {"kg","lbs"};
		String [] spinnerHeValues = {"cm","inc"};
		String [] spinnerWaValues = {"cm","inc"};
		spinner = (Spinner) findViewById(R.id.spinsex);
		spinnerWe = (Spinner) findViewById(R.id.spinwe);
		spinnerHe = (Spinner) findViewById(R.id.spinhe);
		spinnerWa = (Spinner) findViewById(R.id.spinwa);
		spinner.setAdapter(new SpinnerValues(spinnerValues));
		spinnerWe.setAdapter(new SpinnerValues(spinnerWeValues));
		spinnerHe.setAdapter(new SpinnerValues(spinnerHeValues));
		spinnerWa.setAdapter(new SpinnerValues(spinnerWaValues));
		editLabel = (EditText) findViewById(R.id.editlabel);
		editAge = (EditText) findViewById(R.id.editage);
		editWeight = (EditText) findViewById(R.id.editwe);
		editHeight = (EditText) findViewById(R.id.edithe);
		editWaist = (EditText) findViewById(R.id.editwa);
		
		btnOk = (Button) findViewById(R.id.btnok);
		btnOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if((editLabel.getText().toString()!=null && !editLabel.getText().toString().equals(""))
					&& (editAge.getText().toString()!=null && !editAge.getText().toString().equals(""))	
					&& (editWeight.getText().toString()!=null && !editWeight.getText().toString().equals(""))	
					&& (editHeight.getText().toString()!=null && !editHeight.getText().toString().equals(""))	
					&& (editWaist.getText().toString()!=null && !editWaist.getText().toString().equals(""))){
				    if(editMode==false)
				        profileDetails = new ProfileDetails();				    
					profileDetails.setProfileLabel(editLabel.getText().toString());					
					profileDetails.setAge(editAge.getText().toString());
					profileDetails.setSex(spinner.getSelectedItem().toString());
					profileDetails.setProfileWeight(editWeight.getText().toString());
					profileDetails.setProfileHeight(editHeight.getText().toString());
					profileDetails.setProfileWaist(editWaist.getText().toString());
					profileDetails.setWeParam(spinnerWe.getSelectedItem().toString());
					profileDetails.setHeParam(spinnerHe.getSelectedItem().toString());
					profileDetails.setWaParam(spinnerWa.getSelectedItem().toString());
					
					if(editMode==false)
					    profileDAO.createProfile(ProfileCreationActivity.this, profileDetails);
					else
					    profileDAO.updateProfileAccount(ProfileCreationActivity.this, profileDetails);
					Toast.makeText(ProfileCreationActivity.this, "Profile saved", Toast.LENGTH_LONG).show();
					finish();
					
				}else{
					Toast.makeText(ProfileCreationActivity.this, "All fields are required", Toast.LENGTH_LONG).show();
				}
				
			}
		});
		btnCancel = (Button) findViewById(R.id.btncancel);
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Builder yesNo = new AlertDialog.Builder(ProfileCreationActivity.this);
				yesNo.setTitle("Create Profile").setMessage("Are you sure?").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {	
						dialog.dismiss();
						Toast.makeText(ProfileCreationActivity.this, "You can always create a profile", Toast.LENGTH_SHORT).show();						
						finish();					}
				}).setNegativeButton("No", null).show();
				
			}
		});
		Bundle b = getIntent().getExtras();
		if(b.getBoolean("edit",false)==true){
		    profileDetails = profileDAO.getProfileDetails(ProfileCreationActivity.this, 0);
		    if(profileDetails!=null){
		        editLabel.setText(profileDetails.getProfileLabel());		        
		        editAge.setText(profileDetails.getAge());		        
		        editWeight.setText(profileDetails.getProfileWeight());
		        editHeight.setText(profileDetails.getProfileHeight());
		        editWaist.setText(profileDetails.getProfileWaist());
		        spinner.setSelection(profileDetails.getSex().equals("Male")?0:1);
		        spinnerWe.setSelection(profileDetails.getWeParam().equals("kg")?0:1);
		        spinnerHe.setSelection(profileDetails.getHeParam().equals("cm")?0:1);
		        spinnerWa.setSelection(profileDetails.getWaParam().equals("cm")?0:1);
		        editMode = true;
		    }
		}
		
	}
	private class SpinnerValues extends BaseAdapter{
		private String[]values=null;
		
		public SpinnerValues(String[]values){
			this.values = values;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return values.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return values[arg0];
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			TextView textView = new TextView(ProfileCreationActivity.this);
			textView.setText(values[arg0]);
			textView.setTextColor(Color.BLACK);
			textView.setHeight(25);
			textView.setGravity(Gravity.CENTER);
			textView.setId(arg0);
			return textView;
		}
		
	}
}
